home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / markupbase.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  9KB  |  310 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Shared support for scanning document type declarations in HTML and XHTML.
  5.  
  6. This module is used as a foundation for the HTMLParser and sgmllib
  7. modules (indirectly, for htmllib as well).  It has no documented
  8. public API and should not be used directly.
  9.  
  10. '''
  11. import re
  12. _declname_match = re.compile('[a-zA-Z][-_.a-zA-Z0-9]*\\s*').match
  13. _declstringlit_match = re.compile('(\\\'[^\\\']*\\\'|"[^"]*")\\s*').match
  14. _commentclose = re.compile('--\\s*>')
  15. _markedsectionclose = re.compile(']\\s*]\\s*>')
  16. _msmarkedsectionclose = re.compile(']\\s*>')
  17. del re
  18.  
  19. class ParserBase:
  20.     '''Parser base class which provides some common support methods used
  21.     by the SGML/HTML and XHTML parsers.'''
  22.     
  23.     def __init__(self):
  24.         if self.__class__ is ParserBase:
  25.             raise RuntimeError('markupbase.ParserBase must be subclassed')
  26.         self.__class__ is ParserBase
  27.  
  28.     
  29.     def error(self, message):
  30.         raise NotImplementedError('subclasses of ParserBase must override error()')
  31.  
  32.     
  33.     def reset(self):
  34.         self.lineno = 1
  35.         self.offset = 0
  36.  
  37.     
  38.     def getpos(self):
  39.         '''Return current line number and offset.'''
  40.         return (self.lineno, self.offset)
  41.  
  42.     
  43.     def updatepos(self, i, j):
  44.         if i >= j:
  45.             return j
  46.         rawdata = self.rawdata
  47.         nlines = rawdata.count('\n', i, j)
  48.         if nlines:
  49.             self.lineno = self.lineno + nlines
  50.             pos = rawdata.rindex('\n', i, j)
  51.             self.offset = j - pos + 1
  52.         else:
  53.             self.offset = self.offset + j - i
  54.         return j
  55.  
  56.     _decl_otherchars = ''
  57.     
  58.     def parse_declaration(self, i):
  59.         rawdata = self.rawdata
  60.         j = i + 2
  61.         if not rawdata[i:j] == '<!':
  62.             raise AssertionError, 'unexpected call to parse_declaration'
  63.         if rawdata[j:j + 1] == '>':
  64.             return j + 1
  65.         if rawdata[j:j + 1] in ('-', ''):
  66.             return -1
  67.         n = len(rawdata)
  68.         if rawdata[j:j + 2] == '--':
  69.             return self.parse_comment(i)
  70.         if rawdata[j] == '[':
  71.             return self.parse_marked_section(i)
  72.         (decltype, j) = self._scan_name(j, i)
  73.         if j < 0:
  74.             return j
  75.         while j < n:
  76.             c = rawdata[j]
  77.             if c == '>':
  78.                 data = rawdata[i + 2:j]
  79.                 return j + 1
  80.             if c in '"\'':
  81.                 m = _declstringlit_match(rawdata, j)
  82.                 if not m:
  83.                     return -1
  84.                 j = m.end()
  85.             elif c in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ':
  86.                 (name, j) = self._scan_name(j, i)
  87.             elif c in self._decl_otherchars:
  88.                 j = j + 1
  89.             elif c == '[':
  90.                 if decltype == 'doctype':
  91.                     j = self._parse_doctype_subset(j + 1, i)
  92.                 elif decltype in ('attlist', 'linktype', 'link', 'element'):
  93.                     self.error("unsupported '[' char in %s declaration" % decltype)
  94.                 else:
  95.                     self.error("unexpected '[' char in declaration")
  96.             else:
  97.                 self.error('unexpected %r char in declaration' % rawdata[j])
  98.             if j < 0:
  99.                 return j
  100.             continue
  101.             j < 0
  102.         return -1
  103.  
  104.     
  105.     def parse_marked_section(self, i, report = 1):
  106.         rawdata = self.rawdata
  107.         if not rawdata[i:i + 3] == '<![':
  108.             raise AssertionError, 'unexpected call to parse_marked_section()'
  109.         (sectName, j) = self._scan_name(i + 3, i)
  110.         if j < 0:
  111.             return j
  112.         if sectName in ('temp', 'cdata', 'ignore', 'include', 'rcdata'):
  113.             match = _markedsectionclose.search(rawdata, i + 3)
  114.         elif sectName in ('if', 'else', 'endif'):
  115.             match = _msmarkedsectionclose.search(rawdata, i + 3)
  116.         else:
  117.             self.error('unknown status keyword %r in marked section' % rawdata[i + 3:j])
  118.         if not match:
  119.             return -1
  120.         if report:
  121.             j = match.start(0)
  122.             self.unknown_decl(rawdata[i + 3:j])
  123.         
  124.         return match.end(0)
  125.  
  126.     
  127.     def parse_comment(self, i, report = 1):
  128.         rawdata = self.rawdata
  129.         if rawdata[i:i + 4] != '<!--':
  130.             self.error('unexpected call to parse_comment()')
  131.         
  132.         match = _commentclose.search(rawdata, i + 4)
  133.         if not match:
  134.             return -1
  135.         if report:
  136.             j = match.start(0)
  137.             self.handle_comment(rawdata[i + 4:j])
  138.         
  139.         return match.end(0)
  140.  
  141.     
  142.     def _parse_doctype_subset(self, i, declstartpos):
  143.         rawdata = self.rawdata
  144.         n = len(rawdata)
  145.         j = i
  146.         while j < n:
  147.             c = rawdata[j]
  148.             if c == '<':
  149.                 s = rawdata[j:j + 2]
  150.                 if s == '<':
  151.                     return -1
  152.                 if s != '<!':
  153.                     self.updatepos(declstartpos, j + 1)
  154.                     self.error('unexpected char in internal subset (in %r)' % s)
  155.                 
  156.                 if j + 2 == n:
  157.                     return -1
  158.                 if j + 4 > n:
  159.                     return -1
  160.                 (name, j) = self._scan_name(j + 2, declstartpos)
  161.                 if j == -1:
  162.                     return -1
  163.                 meth = getattr(self, '_parse_doctype_' + name)
  164.                 j = meth(j, declstartpos)
  165.                 if j < 0:
  166.                     return j
  167.                 continue
  168.             j < 0
  169.             if c == '%':
  170.                 if j + 1 == n:
  171.                     return -1
  172.                 (s, j) = self._scan_name(j + 1, declstartpos)
  173.                 if j < 0:
  174.                     return j
  175.                 if rawdata[j] == ';':
  176.                     j = j + 1
  177.                 
  178.             rawdata[j] == ';'
  179.             if c == ']':
  180.                 j = j + 1
  181.                 while j < n and rawdata[j].isspace():
  182.                     j = j + 1
  183.                     continue
  184.                     j < 0
  185.                 if j < n:
  186.                     if rawdata[j] == '>':
  187.                         return j
  188.                     self.updatepos(declstartpos, j)
  189.                     self.error('unexpected char after internal subset')
  190.                 else:
  191.                     return -1
  192.             j < n
  193.             if c.isspace():
  194.                 j = j + 1
  195.                 continue
  196.             j + 1 == n
  197.             self.updatepos(declstartpos, j)
  198.             self.error('unexpected char %r in internal subset' % c)
  199.             continue
  200.             None if name not in ('attlist', 'element', 'entity', 'notation') else j + 2 == n if rawdata[j:j + 4] == '<!--' else j + 4 > n
  201.         return -1
  202.  
  203.     
  204.     def _parse_doctype_element(self, i, declstartpos):
  205.         (name, j) = self._scan_name(i, declstartpos)
  206.         if j == -1:
  207.             return -1
  208.         rawdata = self.rawdata
  209.         if '>' in rawdata[j:]:
  210.             return rawdata.find('>', j) + 1
  211.         return -1
  212.  
  213.     
  214.     def _parse_doctype_attlist(self, i, declstartpos):
  215.         rawdata = self.rawdata
  216.         (name, j) = self._scan_name(i, declstartpos)
  217.         c = rawdata[j:j + 1]
  218.         if c == '':
  219.             return -1
  220.         if c == '>':
  221.             return j + 1
  222.         while None:
  223.             (name, j) = self._scan_name(j, declstartpos)
  224.             if j < 0:
  225.                 return j
  226.             c = rawdata[j:j + 1]
  227.             if c == '':
  228.                 return -1
  229.             if c == '(':
  230.                 if ')' in rawdata[j:]:
  231.                     j = rawdata.find(')', j) + 1
  232.                 else:
  233.                     return -1
  234.                 if rawdata[j:j + 1].isspace():
  235.                     j = j + 1
  236.                     continue
  237.                 c == ''
  238.             if not rawdata[j:]:
  239.                 return -1
  240.         rawdata[j:]
  241.         (name, j) = self._scan_name(j, declstartpos)
  242.         c = rawdata[j:j + 1]
  243.         if not c:
  244.             return -1
  245.         if c == '>':
  246.             return j + 1
  247.         continue
  248.  
  249.     
  250.     def _parse_doctype_notation(self, i, declstartpos):
  251.         (name, j) = self._scan_name(i, declstartpos)
  252.         if j < 0:
  253.             return j
  254.         rawdata = self.rawdata
  255.         while None:
  256.             c = rawdata[j:j + 1]
  257.             if not c:
  258.                 return -1
  259.             (name, j) = self._scan_name(j, declstartpos)
  260.             if j < 0:
  261.                 return j
  262.             continue
  263.             return None
  264.  
  265.     
  266.     def _parse_doctype_entity(self, i, declstartpos):
  267.         rawdata = self.rawdata
  268.         if rawdata[i:i + 1] == '%':
  269.             j = i + 1
  270.             while None:
  271.                 c = rawdata[j:j + 1]
  272.                 break
  273.                 continue
  274.         rawdata[i:i + 1] == '%'
  275.         j = i
  276.         (name, j) = self._scan_name(j, declstartpos)
  277.         if j < 0:
  278.             return j
  279.         while None:
  280.             c = self.rawdata[j:j + 1]
  281.             if c == '>':
  282.                 return j + 1
  283.             (name, j) = self._scan_name(j, declstartpos)
  284.             if j < 0:
  285.                 return j
  286.             continue
  287.             return None
  288.  
  289.     
  290.     def _scan_name(self, i, declstartpos):
  291.         rawdata = self.rawdata
  292.         n = len(rawdata)
  293.         if i == n:
  294.             return (None, -1)
  295.         m = _declname_match(rawdata, i)
  296.         if m:
  297.             s = m.group()
  298.             name = s.strip()
  299.             if i + len(s) == n:
  300.                 return (None, -1)
  301.             return (name.lower(), m.end())
  302.         self.updatepos(declstartpos, i)
  303.         self.error('expected name token at %r' % rawdata[declstartpos:declstartpos + 20])
  304.  
  305.     
  306.     def unknown_decl(self, data):
  307.         pass
  308.  
  309.  
  310.